<?php

namespace JsonRpc\Services\Product;

use JsonRpc\Models\PGoods;
use JsonRpc\Services\CommonService;
use JsonRpc\Services\System\UserService;

/**
 *  用户
 * @author walkor <worker-man@qq.com>
 */
class GoodsService
{
    use CommonService;

    private static function getBaseModel()
    {
        return "JsonRpc\\Models\\PGoods";
    }

    static function getInfoList($params = [])
    {
        $clazz = self::getBaseModel();
        $model  = new $clazz();
        if($model->has_client){
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            $model = $model->where('client_id', $user['client_id']);
        }

        if(isset($params['sortby']) || !empty($params['sortby'])){
            if(is_array($params['sortby'])){
                foreach ($params['sortby'] as $sortBy){
                    if(is_array($sortBy)){
                        $model = $model->orderBy($sortBy[0], $sortBy[1]);
                    }else{
                        $model = $model->orderBy($sortBy);
                    }
                }
            }else{
                $model = $model->orderBy($params['sortby']);
            }
        }

        //菜单数据控制
        if(isset($params['curr_menu_id'])){
            $rules = UserService::getMenuDataRule(['menu_id'=>$params['curr_menu_id'], 'user_id'=>$params['curr_user_id']]);
            if(!empty($rules)){
                if(is_array($rules)){
                    foreach ($rules as $rule){
                        $model = $model->whereRaw($rule);
                    }
                }else{
                    $model = $model->whereRaw($rules);
                }
            }
        }

        if(isset($params['page'])){
            $page = isset($params['page']) ? intval($params['page']) : 1;
            $rows = isset($params['rows']) ? intval($params['rows']) : 10;
            $total = $model->toBase()->getCountForPagination();
            $info = $model->forPage($page, $rows)->get(isset($params['select'])?$params['select']:['*']);

            if($info){
                $list = $info->toArray();
            }else{
                $list = [];
            }
            return ['total'=>$total, 'rows'=>$list];
        }

        $info = $model->get(isset($params['select'])?$params['select']:['*']);
        if($info){
            return $info->toArray();
        }
        return [];
    }

    public static function ping()
    {
        return 'OK';
    }

    public static function saveData($params)
    {
        if (isset($params['id']) && $params['id']) {
            //更新
            $model = PGoods::find($params['id']);
            $model->updated_by = $params['curr_user_id'];

        } else {
            $user = UserService::getInfoById(['id' => $params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new PGoods();
            $model->client_id = 0;
            $model->org_id = 0;
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }
        $model->goods_name = trim($params['name']);
        $model->category_id = trim($params['category_id']);
        $model->category_name = trim($params['category_name']);
        $model->brand_id = trim($params['brand_id']);
        $model->brand_name= trim($params['brand_name']);
        $model->label_ids = trim($params['label_ids']);
        $model->label_names = trim($params['label_names']);
        $model->goods_spec = trim($params['goods_spec']);
        $model->manufacturers_url = trim($params['manufacturers_url']);
        $model->goods_price = floatval(trim($params['goods_price']));
        $model->goods_state = trim($params['goods_state']);
        if(!empty($params['goods_image'])) $model->goods_image = $params['goods_image']; //不为空时才保存图片
        return $model->save();
    }
    static function adjust($params){
        if(empty($params['id'])){
            throw  new \Exception('商品id不能为空');
        }
        $model = PGoods::find($params['id']);
        $model->goods_price = floatval(trim($params['goods_price']));
        return $model->save();
    }

    //ajax获取商品列表
    public function getGoodsList($params)
    {
        if (empty($params['key'])) {
            $res = [];
        }else{
            $goods_name = $params['key'];
            $res = PGoods::where('goods_name','like','%'.$goods_name.'%')->get();
        }

        return $res;
    }
}
